package com.yeyks.controller.other;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.dto.RechargeRecordDTO;
import com.yeyks.common.param.RechargeRecordQueryParam;
import com.yeyks.common.result.RechargeRecordResult;
import com.yeyks.common.result.user.UserInfoByParamResult;
import com.yeyks.common.utils.DateUtils;
import com.yeyks.common.utils.excel.ExcelUtil;
import com.yeyks.consumer.service.RechargeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * @author: dagny
 * @date: 2019/12/23 0023 13:33
 */
@Api(tags = "v4.0.0充值记录", description = "相关用户充值记录信息")
@RestController
@RequestMapping(value = "/recharge/record")
public class RechargeRecordController {

    @Autowired
    private RechargeRecordService rechargeRecordService;

    @ApiOperation("V4.0.0 分页查询用户充值记录")
    @PostMapping("/query")
    public AjaxResponseBody queryByParam(@Validated RechargeRecordQueryParam param) {
        param.setChargeType(4); //查询vip充值记录
        param.setStatus(2);
        AjaxResponseBody body = null;
        List<RechargeRecordDTO> list = rechargeRecordService.queryByParam( param );
        int count = rechargeRecordService.queryCount( param );
        HashMap<String, Object> map = Maps.newHashMap( );
        map.put( "list", list );
        map.put( "count", count );
        body = AjaxResponseBody.success( map );
        return body;
    }

    @ApiOperation("V4.0.0 导出充值记录信息")
    @PostMapping("/down/list")
    public void downRechargeRecord(@Validated RechargeRecordQueryParam param, HttpServletResponse response) {
        param.setChargeType(4); //查询vip充值记录
        param.setStatus(2);
        List<RechargeRecordDTO> list = rechargeRecordService.queryExportByParam( param );
        if ( !CollectionUtils.isEmpty( list ) ) {
            List<RechargeRecordResult> results = Lists.newArrayList( );
            list.forEach( rechargeRecordDTO -> {
                RechargeRecordResult result = new RechargeRecordResult( );
                result.setId( rechargeRecordDTO.getId( ) );
                result.setPhone( rechargeRecordDTO.getPhone( ) );
                result.setCardNo( rechargeRecordDTO.getCardNo() );
                result.setOrderNo(rechargeRecordDTO.getOrderNo());
                if ( rechargeRecordDTO.getUserType( ) == 1 ) {
                    result.setUserType( "个体用户" );
                } else {
                    result.setUserType( "企业用户" );
                }
                if ( rechargeRecordDTO.getType( ) == 10 ) {
                    result.setType( "手动充值" );
                } else {
                    result.setType( "线上充值" );
                }
                if(null != rechargeRecordDTO.getPayAmount()){
                    result.setPayAmount( String.valueOf((Double.parseDouble(rechargeRecordDTO.getPayAmount().toString())/100) ) );
                }else{
                    result.setPayAmount( "0" );
                }
                if(null != rechargeRecordDTO.getGiveAmount()){
                    result.setGiveAmount( String.valueOf((Double.parseDouble(rechargeRecordDTO.getGiveAmount().toString())/100) ) );
                }else{
                    result.setGiveAmount( "0" );
                }
                result.setCreatedTime( DateUtils.formatYMDHMSStr( rechargeRecordDTO.getCreatedTime( ) ) );
                results.add( result );
            } );
            ExcelUtil.disposeWriteExcelWithSheets(response, results, new RechargeRecordResult());
        }
    }
}
